package com.android.vgongyi.parser;

import java.io.InputStream;
import java.util.ArrayList;

import android.content.Context;

import com.android.vgongyi.dao.BaseDAO;
import com.android.vgongyi.dao.CommentDAO;
import com.android.vgongyi.entity.CommentBean;
import com.android.vgongyi.entity.CommentEntity;
import com.android.vgongyi.service.VGongyiException;
import com.android.vgongyi.utils.DBUtil;
import com.android.vgongyi.utils.StringUtil;
import com.google.gson.Gson;

/**
 * parser for channel view
 * */
public class CommentParser extends BaseParser{

	public ArrayList<CommentEntity> executeToObject(InputStream in, Context context) throws VGongyiException {
		ArrayList<CommentEntity> channelList = new ArrayList<CommentEntity>();
		String json = StringUtil.converStreamToString(in);
		Gson gson =new Gson();
		channelList = gson.fromJson(json, CommentBean.class).getList();
		if(channelList == null){
			return channelList;
		}
		CommentDAO commentDAO = new CommentDAO(context);
		for(int i = 0 ;i < channelList.size();i++){
			if(!DBUtil.checkisExist(BaseDAO.URI_COMMENT, "id="+"'"+channelList.get(i).getId()+"'", context)){
				commentDAO.insert(channelList.get(i));
			}
		}
		return channelList;
	}

}
